Repository navigation
Conversation
This was referenced Nov 17, 2026
A request asking for cliffs finds or inserts one CliffImpact per simulation run its report run names, in the report run's own transaction: pending once, then joined; a failed row never joined; a succeeded row is the run's result; two transactions at once insert one. A calculation the database refuses leaves no report. The store records a calculation's Modal call, or fails it, while it is pending with none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A create or re-run asking for cliffs proposes its sides' calculation IDs (ProposedCliffs). In the transaction that settled its report run, find_or_insert_report_run_cliffs finds or inserts the CliffImpact of each simulation run that report run names, against uq_cliff_impacts_unfailed, in simulation run ID order, so a calculation the database refuses leaves nothing written. The economy store records a calculation's Modal call, or fails it, while it is pending with none. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A report created or re-run with include_cliffs writes its runs' cliff calculations with its report run and starts, before the coordinator, only those it created, through the country's simulation function with a context naming the calculation. Two identical requests start one per run; include_cliffs is in no key, stored input or calculation option; a refused cliff call fails that calculation and the report is answered 201. The 422s of C1 · 24's interim answers A and B go, a stored report asking for cliffs asks for its new runs' cliffs, and a budget window's or household report's re-run asking for them is refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
POST /reports with include_cliffs, and a re-run with the re-run body's new include_cliffs, propose their sides' calculation IDs with their report run. Before the report run's coordinator, the service starts only the calculations its request inserted: the country's simulation function, given the run's own input and a context naming the cliff calculation. A refused call fails that calculation and the report is still answered 201, naming each side's calculation and status in cliffs, a field left out when the request didn't ask. include_cliffs is never stored, keyed or in any calculation's options. The 422s of C1 · 24's interim answers A and B go: a report stored before keys asking for cliffs is re-run with its runs' cliffs, and a budget window's or household report's re-run asking for them is refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Simulation Entrypoint's README describes what a create or re-run asking for cliffs does and answers, and drops the interim 422s; the persistence README names the cliff calculation lookup and its writes; the Stage 12 foundation document says the service now starts each run's cliff calculation. Adds the changelog fragment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…iling) From the ORM redo's review: a re-run asking for cliffs returns its report run and their calculations together, leaving FoundReportRun as every other lookup returns it. Also covers the simulation run ID order of the inserts, a request losing the report run taking the winner's runs' calculations, a re-run over two distinct simulation runs, and a refused calculation naming the column that refused it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the ORM redo's review: insert_report_rerun and FoundReportRun stay as every other caller uses them, and a re-run asking for cliffs goes through insert_report_rerun_with_cliffs, which returns its report run and their calculations together (FoundReportRerun). A cliff calculation naming a missing run now names cliff_impacts.simulation_run_id in EconomyRecordReferenceError, and the run classes' comments include it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each log of a cliff calculation's unconfirmed call, unrecorded call ID or unwritten failure carries the error type and the cliff_impacts row through log_extra, the only fields that reach Cloud Logging. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The three logs of a cliff calculation's call now reach Cloud Logging, with the error's type and the cliff_impacts row, as #1092's rule requires of every Simulation Entrypoint log. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
anth-volk
force-pushed
the
v2plan/c1-38-run-cliff-results
branch
from
November 19, 2026 17:09
fc6989e to
495bf7e
Compare
anth-volk
force-pushed
the
v2plan/c1-39-include-cliffs
branch
from
November 19, 2026 17:09
9fab3d2 to
49858c3
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1098
POST /reports(economy) andPOST /reports/{report_id}/runsacceptinclude_cliffs: trueagain. A request with it starts a cliff calculation for each side's simulation run that has none, andinclude_cliffsis never stored or keyed.Stacked on #1096 (C1 · 38: the v2 worker calculates a simulation run's cliff result), and based on its branch. Retarget to
mainonce #1096 merges.Deploy order:
bc334f9877b7(cliff_impacts, Store one cliff result per simulation run in the v2 database, with grants policyengine-api#4327, C1 · 37) must be applied in each environment first.What a request asking for cliffs does
A simulation run has one cliff result, its cliff gap and cliff share, in a
cliff_impactsrow that every report naming the run reads (C1 · 24's answer 4b).Rows. The create or re-run ends on its report run as before, created or joined. In the same transaction, it finds or inserts the cliff calculation of each simulation run that report run names (
find_or_insert_report_run_cliffs):INSERT … ON CONFLICT (simulation_run_id) WHERE status <> 'failed' DO NOTHINGagainstuq_cliff_impacts_unfailed, in simulation run ID order;The rows go through the
CliffImpactmapped class on the request's SQLAlchemySession, as the rest of the service's database code does since the ORM conversion (Answer 500 when a household job's commit is unconfirmed, and move sim's database code to the SQLAlchemy ORM #1090). A re-run asking for cliffs goes throughinsert_report_rerun_with_cliffs, which returnsFoundReportRerun: its report run and its calculations.Two identical requests at once end on one calculation per run. A request the database refuses wrote neither the report run nor its calculations, so a 503
database_unavailablestill means nothing was written.Calls. Only for a calculation it inserted, and before the report run's coordinator, the service spawns the country's simulation function in its cliff mode (Calculate each simulation run's cliff gap and share as its own cliff result #1096). The payload is the run's own
SimulationExecutionInput, built as a standalone run's; the context names the calculation (cliff_result_id) under the run's artifact prefix. The service then records the call's ID while the row is pending with none. A calculation runs beside its simulation run, whatever that run's state, and recalculates the simulation from its dataset and policy.Refused calls. When Modal refuses a cliff call, or doesn't confirm it in 10 s, the service fails that row while it's still pending with no call. The answer is still 201, with that side
failed, and the next request asking for cliffs starts another. Because cliff calls start first, a coordinator that Modal refuses (503modal_call_refused) still leaves them started. The three log records for these problems (cliff_calculation_call_unconfirmed,cliff_calculation_not_failed,cliff_calculation_call_not_recorded) pass their fields throughlog_extraand name thecliff_impactsrow withdatabase_row_attributes, as Name the database row and Modal call in v2 telemetry, and emit the Simulation Entrypoint's log fields #1092's logging rule requires.Answer.
EconomyReportCreatedgainscliffs: each side'scliff_result_idandstatus. The field is left out when the request didn't ask.include_cliffsis never stored or keyedEconomyReportRequest.options(),BudgetWindowReportRequest.options()andsimulation_optionsno longer carry it. The coordinator's payload options hold only the SPM selection, and the coordinator never plans cliff columns.reports.inputsare unchanged by it, which the tests check.inputsholdinclude_cliffs: trueused to answer 422 on re-run. Its re-run now asks for its runs' cliffs. Its stored option is only read, andrerun_scopeno longer passes it to the coordinator.Refusals
cliffs_not_availableonPOST /reports, and on a re-run of such a stored report, is gone (C1 · 24's interim answers A and B).POST /reports/{report_id}/runstakesinclude_cliffs. A budget window's or household report's re-run asking for it is refused with 422cliffs_not_availableat["body", "include_cliffs"], since neither calculates cliffs. A budget window's create already refuses the field.Tests
Red-green throughout: each "(failing)" commit precedes its change. Only the tests this PR adds or changes were run, each by node ID, not whole files or suites. The Postgres tests ran as the simulation service's role, against a database built from #4327's branch (
a4be63110). In CI they needpublic-api-committo name #4327's merge, which #1096 moves to.test_cliff_calculations.py, 17 tests):cliff_impacts.simulation_run_idcolumn in its error;inputsdon't change with the flag, and no coordinator payload carries it;log_extra(test_log_fields.py);Sim's checks (pinned Ruff, rules, quality guards, lock checks, Pyright for the entry and persistence packages) pass, and so does the changelog fragment check against #1096's head.
Commits
The change was first written on SQLAlchemy Core and has been rebuilt on mapped classes and Sessions on top of #1096's ORM version. Each "(failing)" commit adds its tests before the change that makes them pass.
1232cb68(failing),ad1442b9: persistence, each run's cliff calculation written with its report run, on ORM Sessions.4c727839(failing),1d0b7825: the Simulation Entrypoint starts each side's cliff calculation.7c591174: READMEs, the migration document and the changelog fragment.721f775e(failing),02e9f483: a re-run's cliffs returned beside its run, insert order, and the refused column named.f2382482(failing),0a8115fc: the cliff-call problem logs go throughlog_extra.0c5206d7: this PR's text callsPolicyEngine/policyengine-api"API v1".49858c36: the log test states each case's error and fails on a row logged twice.Review
Four read-only review rounds:
FoundReportRunis as onmain, and there are new tests of the insert order, a request losing the report run, and a re-run over two different simulation runs.Deployment and environment variables
No workflow, Dockerfile, Terraform,
v2_app.pyor staging check script changes, and the code reads no new environment variable or secret. The service's privileges oncliff_impacts, which/readychecks, come from #1096'saccess.py.🤖 Generated with Claude Code